local hx__taohui = fk.CreateSkill {
  name = "hx__taohui",
}

local HX = require "packages/konglong/hxfs2"

hx__taohui:addEffect(fk.EventPhaseStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart and player:hasSkill(hx__taohui.name) then
      return player.phase == Player.Start
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player.maxHp
    local draw = 0
    local equip = 0
    local hasRedCard = false
    local hasBlackCard = false
    if #player:getCardIds("he") > 0 then
      local cards = room:askToChooseCards(player,{
        target = player,
        min = 1,
        max = x,
        skill_name = hx__taohui.name,
        flag = "he",
      })
      if #cards > 0 then
        room:recastCard(cards, player, hx__taohui.name)
        for _, id in ipairs(cards) do
          local card = Fk:getCardById(id)
          if Fk:getCardById(id).type == Card.TypeTrick then
            draw = draw + 1
          end
          if Fk:getCardById(id).type == Card.TypeEquip then
            equip = equip + 1
          end
          if card.color == Card.Red then
            hasRedCard = true
          end
          if card.color == Card.Black then
            hasBlackCard = true
          end
        end
        if draw > 0 then
          HX.DrawCardFromPile(room, player, hx__taohui.name, 1)
        end
        if equip > 0 then
          room:addPlayerMark(player, "@hx__taohui-card", equip)
        end
      end
    end
    if hasRedCard then
      player:drawCards(1, hx__taohui.name)
      room:changeMaxHp(player, 1)
    end
    if player.room:askToSkillInvoke(player, {skill_name = hx__taohui.name, prompt = "韬晦：你可以视为使用一张【决斗】(若重铸了黑色牌则该【决斗】的目标角色无法使用红色牌响应)"}) then
      local to = room:askToChoosePlayers(player,{
        targets = room:getOtherPlayers(player, false),
        min_num = 1,
        max_num = 1,
        skill_name = hx__taohui.name,
        prompt = "#hx__taohui-invoke",
        cancelable = true,
      })
      if #to == 0 then return end
      local p = to[1]
      if hasBlackCard then
        room:addPlayerMark(p, "@@hx__taohui-mark", 1)
      end
      if p then
        room:useVirtualCard("duel", nil, player, {p}, hx__taohui.name, false)
        room:setPlayerMark(p,"@@hx__taohui-mark", 0)
      end
    end
  end,
})

hx__taohui:addEffect(fk.DamageInflicted, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return data.from
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player.maxHp
    local draw = 0
    local equip = 0
    local hasRedCard = false
    local hasBlackCard = false
    if #player:getCardIds("he") > 0 then
      local cards = room:askToChooseCards(player,{
        target = player,
        min = 1,
        max = x,
        skill_name = hx__taohui.name,
        flag = "he",
      })
      if #cards > 0 then
        room:recastCard(cards, player, hx__taohui.name)
        for _, id in ipairs(cards) do
          local card = Fk:getCardById(id)
          if Fk:getCardById(id).type == Card.TypeTrick then
            draw = draw + 1
          end
          if Fk:getCardById(id).type == Card.TypeEquip then
            equip = equip + 1
          end
          if card.color == Card.Red then
            hasRedCard = true
          end
          if card.color == Card.Black then
            hasBlackCard = true
          end
        end
        if draw > 0 then
          HX.DrawCardFromPile(room, player, hx__taohui.name, 1)
        end
        if equip > 0 then
          room:addPlayerMark(player, "@hx__taohui-card", equip)
        end
      end
    end
    if hasRedCard then
      player:drawCards(1, hx__taohui.name)
      room:changeMaxHp(player, 1)
    end
    if player.room:askToSkillInvoke(player, {skill_name = hx__taohui.name, prompt = "韬晦：你可以视为使用一张【决斗】(若重铸了黑色牌则该【决斗】的目标角色无法使用红色牌响应)"}) then
      local to = room:askToChoosePlayers(player,{
        targets = room:getOtherPlayers(player, false),
        min_num = 1,
        max_num = 1,
        skill_name = hx__taohui.name,
        prompt = "#hx__taohui-invoke",
        cancelable = true,
      })
      if #to == 0 then return end
      local p = to[1]
      if hasBlackCard then
        room:addPlayerMark(p, "@@hx__taohui-mark", 1)
      end
      if p then
        room:useVirtualCard("duel", nil, player, {p}, hx__taohui.name, false)
        room:setPlayerMark(p,"@@hx__taohui-mark", 0)
      end
    end
  end,
})


hx__taohui:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if player:getMark("@@hx__taohui-mark") > 0 and card:getColorString() == "red" then
      return true
    end
    return false
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@hx__taohui-mark") > 0 and card:getColorString() == "red" then
      return true
    end
    return false
  end,
})

hx__taohui:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(hx__taohui.name) then
      return player:getMark("@hx__taohui-card")
    end
    return 0
  end,
})

return hx__taohui 